package uiLayer;
import controlLayer.*;
import modelLayer.*;
import java.util.Scanner;

public class CustomerUI {
   private CustomerCtr customerCtr;
    
   public CustomerUI() {
       customerCtr = new CustomerCtr();
    }
    
   /**
     * Customers menu methods
     */
    public void customersMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeCustomersMenu();
            
            switch(choice) {
            case 1 : createCustomer();
                break;
            case 2 : findCustomer();
                break;
            case 3 : updateCustomer();
                break;
            case 4 : removeCustomer();
                break;
            case 0 : stop = true;
                break;
            default : System.out.println("Invalid command");
                      MainMenuUI.pause();
                break;
            }
        }
    } 
    
    private int writeCustomersMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Customers Menu ***");
        System.out.println("1. Add customer ");
        System.out.println("2. Find customer ");
        System.out.println("3. Update customer ");
        System.out.println("4. Remove customer ");
        System.out.println("0. Return to main menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }
    
    /**
     * Customers information input
     */
    private String inputName() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the name : ");
        String name = keyboard.nextLine();
        if (!name.isEmpty()) {
            return name;
        }
        else {
            return inputName();
        }  
    }
    
    private String inputPhone() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the phone number of the person : ");
        String phone = keyboard.nextLine();
        if (!phone.isEmpty()) {
            return phone;
        }
        else {
            return inputPhone();
        }  
    }
    
    private String inputEmail() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the email : ");
        String email = keyboard.nextLine();
        return email;
    }
    
    private String inputAddress() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the address : ");
        String address = keyboard.nextLine();
        return address;
    }
    
    public int inputCprNo() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the CPR number of the customer : ");
        int cprNo = keyboard.nextInt();
        return cprNo;
    }
    
    /**
     * Customers information show
     */
    private void showName(String name){
        System.out.println("Name : " + name);
    }
    
    private void showPhone(String phone){ 
        System.out.println("Phone : " + phone);
    }
    
    private void showEmail(String email){ 
        System.out.println("Email : " + email);
    }

    private void showAddress(String address) { 
        System.out.println("Address : " + address);
    }
    
    /**
     * find Customer
     */
    private void findCustomer() {
        int cprNo = inputCprNo();
        Customer cust = (customerCtr.findCustomer(cprNo));
        if(cust != null) {
            System.out.println();
            showName(cust.getName());     
            showPhone(cust.getPhone());
            showEmail(cust.getEmail());
            showAddress(cust.getAddress());
            
        }
        else { 
            System.out.println("No customer was found with the specified CPR number.");
        }
        System.out.println();
        MainMenuUI.pause();
        
    }
    
    /**
     * Update customer
     */
    private void updateCustomer() {
        Scanner keyboard = new Scanner(System.in);
        int cprNo = inputCprNo();
        Customer cust = (customerCtr.findCustomer(cprNo));           
        boolean stop = false;
        if (cust != null) {
            System.out.println();
            showName(cust.getName());
            showPhone(cust.getPhone());
            showEmail(cust.getEmail());
            showAddress(cust.getAddress());
            System.out.println();
            System.out.println("1. Update name");
            System.out.println("2. Update phone");
            System.out.println("3. Update email");
            System.out.println("4. Update address");
            System.out.println("0. Return to previous menu");
            System.out.println();
            System.out.println("Enter your choice : ");
            
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                case 1 : cust.setName(inputName());
                    break;
                case 2 : cust.setPhone(inputPhone());
                    break;
                case 3 : cust.setEmail(inputEmail());
                    break;
                case 4 : cust.setAddress(inputAddress());
                    break;
                case 0 : stop= true;
                }
                System.out.println();
                System.out.println("The information was updated");
                System.out.println();
            }
            
        }
        else {
            System.out.println("No customer was found with the specified CPR number.");
        }
        MainMenuUI.pause();
    }
    
    /**
     * remove customer
     */
    private void removeCustomer() {
        Scanner keyboard = new Scanner(System.in);
        int cprNo = inputCprNo();
        Customer cust = (customerCtr.findCustomer(cprNo));
        boolean stop = false;
        if (cust != null) {
            System.out.println();
            showName(cust.getName());
            showAddress(cust.getPhone());
            showEmail(cust.getEmail());
            showAddress(cust.getAddress());
            
            System.out.println();
            System.out.println("1.Remove customer");
            System.out.println("0.Return to previous menu");
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                case 1 : customerCtr.removeCustomer(cprNo);
                        System.out.println();
                        System.out.println("The customer was successfully removed");
                        System.out.println();
                        MainMenuUI.pause();
                    break;
                case 0 : stop = true;
                    break;
                }
            }
        }
        else {
            System.out.println("No customer was found with the specified CPR number.");
        }
    }
    
    /**
     * Friends crerate person
     */
    private void createCustomer() {
        String name = inputName();
        String phone = inputPhone();
        String email = inputEmail();
        String address = inputAddress();
        int cprNo = inputCprNo();
            
        customerCtr.createCustomer(name, phone, email, address, cprNo);
        System.out.println();
        System.out.println("The customer was successfully created");
        System.out.println();
        MainMenuUI.pause();
        }
}